package at.ac.thornwishstalon.ocrapp.main.fragments;

import android.app.Fragment;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.sax.TextElementListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.widget.Toast;
import at.ac.thornwishstalon.ocrapp.R;
import at.ac.thornwishstalon.ocrapp.main.OcrappActionProvider;

public class TextFragment extends Fragment implements OnSharedPreferenceChangeListener{
	private final String TAG="TextFragment";
	private final String PREFS="at.ac.ocrap.prefs";
	public String OCRAPP_TEXT="ocrapp_text";
	private String text;
	private EditText editText;
	private TextView title;

	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.main_fragment_text, container,false);
		editText= (EditText) view.findViewById(R.id.editText1);
		title = (TextView) view.findViewById(R.id.textView1);
		return view;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
		
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
		sharedPref.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SharedPreferences sharedPreferences = getActivity().getSharedPreferences(PREFS, 0);
		
		text= sharedPreferences.getString(OCRAPP_TEXT, "");
		editText.setText(text);
		title.setText(Uri.parse(sharedPreferences.getString("ocrapp_image_uri", "...")).getPath());
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.main_fragment_text, menu);
		MenuItem item = menu.findItem(R.id.main_text_fragment_menu_share);
		
		ShareActionProvider provider = (ShareActionProvider) item.getActionProvider();

		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		shareIntent.setType("text/plain");
		shareIntent.putExtra(Intent.EXTRA_TEXT, text);
		
		provider.setShareIntent(shareIntent);
		//provider.setShareHistoryFileName(null);

	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		Log.d(TAG, "prefs changed");
		if(key.equals(OCRAPP_TEXT)){
			text= sharedPreferences.getString(OCRAPP_TEXT, "");
			editText.setText(text);
		}
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.main_text_fragment_text_mene_copyToClipboard:
			ClipboardManager clip= (ClipboardManager)getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
			ClipData data= ClipData.newPlainText("ocrapp text", editText.getText().toString());
			clip.setPrimaryClip(data);
			
			Toast.makeText(getActivity(), getActivity().getString(R.string.copy_to_clipboard), Toast.LENGTH_SHORT).show();
			
			break;
		}
		
		return true;
	}

	
}
